import 'reflect-metadata';
import { RequestHandler } from 'express';
import MethodType from '../util/MethodUtil';
import { router } from '../util/router';

type MyClassDecorator = <T extends { new (...args: any): any }>(
  targetClass: T
) => void;
export function Controller(reqRootPath: string): MyClassDecorator {
  console.log('类装饰器：control 控制器装饰器');
  return function (targetClass: any) {
    Object.keys(targetClass.prototype).forEach((methodnamekey) => {
      let routerpath = Reflect.getMetadata(
        'path',
        targetClass.prototype,
        methodnamekey
      );
      let methodtype: MethodType = Reflect.getMetadata(
        'methodType',
        targetClass.prototype,
        methodnamekey
      );
      // targetMehodfunc 方法名对应的方法体-control里的methodname：login
      let targetMehodfunc = targetClass.prototype[methodnamekey];
      /**
       * 格式;
       *  router.get('/', (req, res) => {
       *    res.send('登录成功');
       *  });
       */
      if (routerpath && methodtype) {
        router[methodtype](routerpath, targetMehodfunc);
      }
    });
  };
}
